const { lessorsModel } =require('./models/lessorsModel')
const { OrdersModel} =require("./models/ordersModel")
require("./models/parkingsModel")

module.exports.register = async function (data) {
    return await lessorsModel.create(data)
}

module.exports.checkUsername = async function (username) {
    return await lessorsModel.find(username)
}

module.exports.login = async function (username) {
    return await lessorsModel.find(username)
}

module.exports.getLessorsOrders=async function(id){
            const {lessorsId,pageSize,currentPage,type, value,} =id
            const data=await OrdersModel.find({lessorsId:lessorsId})      
                    .populate("tenantsId").populate("parkingsId")
    return data}

module.exports.getLessors = async function (msg) {
    //获取数据总条数
    const total = await lessorsModel.countDocuments();
    //计算总页数
    const pages = Math.ceil(total / msg.pageSize);
    //查询页数
    const chaxunye = await lessorsModel.find({
        [msg.searchType]: { $regex: msg.searchValue, $options: '$i' }
    }).countDocuments();

    const chapages = Math.ceil(chaxunye / msg.pageSize);
    //精确查找
    // const data = await lessorsModel.find(cha);
    //模糊查找
    const tableData = await lessorsModel.find({
        [msg.searchType]: { $regex: msg.searchValue, $options: '$i' }
    })
        //页数
        .limit(msg.pageSize - 0).skip((msg.currentPage - 1) * msg.pageSize);
    return {
        tableData, pages, total, chaxunye, chapages
    }
}

module.exports.getState = async function (msg) {
    const data = await lessorsModel.updateOne({ _id: msg._id }, { state: msg.state });
    return data
}